package com.my.helper.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.my.helper.R;
import com.my.helper.activity.MessageWebViewActivity;
import com.my.helper.adapter.MainAdapter;
import com.my.helper.bean.Contentlist;
import com.my.helper.bean.MainRoot;
import com.my.helper.bean.Pagebean;
import com.my.helper.bean.Showapi_res_body;
import com.my.helper.url.MURL;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 项目名称：Helper
 * 创建人：
 * 创建时间：2018/5/4 11:29
 */

public class FragmentShiWu extends Fragment {
    private View view = null;
    private String TAG = "BMW";
    private RefreshLayout refreshLayout;
    private String url = MURL.ZT_YM + "食物" + MURL.ZT_CONTENT;
    private RecyclerView mRecyclerView;
    private MainAdapter adapter;
    private GridLayoutManager mGridLayoutManager;
    private List<Contentlist> contentlists = new ArrayList<Contentlist>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shi_wu, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        refreshLayout = view.findViewById(R.id.rl_sw);
        mRecyclerView = view.findViewById(R.id.rv_shi_wu);
        mGridLayoutManager = new GridLayoutManager(getActivity(),
                1, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        adapter = new MainAdapter(getActivity());
        mRecyclerView.setAdapter(adapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {// 刷新
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() { // 加载更多
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500, true, true);
            }
        });

        adapter.setListener(new MainAdapter.OnMainClickListener() {
            @Override
            public void onClickListener(int position) {
                goIntent(position);
            }
        });
    }

    private void goIntent(int position) {
        Intent intent = new Intent(getActivity(), MessageWebViewActivity.class);
        intent.putExtra("web_message_title", contentlists.get(position).getTitle());
        intent.putExtra("web_message_url", contentlists.get(position).getUrl());
        intent.putExtra("web_message_type", "饮食");
        intent.putExtra("web_message_news_title", contentlists.get(position).getTitle());

        startActivity(intent);

    }

    private void initData() {
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "食物请求失败:" + e.toString());
                        refreshLayout.finishRefresh(false);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        contentlists.clear();
                        Log.i(TAG, "食物请求成功：" + response);
                        refreshLayout.finishRefresh(true);
                        Gson gson = new Gson();
                        try {
                            MainRoot root = gson.fromJson(response, MainRoot.class);
                            Showapi_res_body body = root.getShowapi_res_body();
                            Pagebean pagebean = body.getPagebean();
                            contentlists = pagebean.getContentlist();

                        } catch (Exception e) {
                            Log.e(TAG, "食物解析错误：" + e.toString());
                        }

                        adapter.setDatas(contentlists);
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}
